import de.bezier.data.sql.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class Gateway{
  ArrayList<Sniffer> m_sniffers = new ArrayList<Sniffer>();
  ArrayList<Node> m_nodes = new ArrayList<Node>();
  MySQL msql;
  
  Gateway(PApplet a_ptvPApplet){
    String user = "root";
    String pass = "";
    String database = "phone_tracker";
    msql = new MySQL( a_ptvPApplet, "localhost", database, user, pass );
    
    // Get Sniffers
    if ( msql.connect() ){
        msql.query( "SELECT * FROM `resultset-location`" );
        int s_id = 0;
        while (msql.next()){
          String time = msql.getString("dt");
          String hostname = msql.getString("hostname");
          int location = msql.getInt("location");
          int suburb = msql.getInt("suburb");
          String postcode = msql.getString("postcode");
          Float latitude = msql.getFloat("latitude");
          Float longitude = msql.getFloat("longitude");
            
          Sniffer each = new Sniffer(s_id,
                                     time,
                                     hostname,
                                     location,
                                     suburb,
                                     postcode,
                                     latitude,
                                     longitude,
                                     "wifi",
                                     "AABBCC");
          log(longitude);
          this.m_sniffers.add(each);     // geo locations
          
          println("Added Sniffer:"+s_id+" Lat:"+latitude+" Long: "+longitude);
          s_id++;
        }
    }else{
        println("connection failed");
        // connection failed !
        // make connecting error and demo screen
        // 
        //ptvSniffers = new BeASniffer(30, 300); // how many fake sniffers ? sniffers, nodes
    }
    
    // Get current time
    String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
    Calendar cal = Calendar.getInstance();
    // 1 hour ago
    cal.add(Calendar.HOUR_OF_DAY, -1);
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
    
    // Get nodes in db
    /* get them in draws
    if ( msql.connect() ){
      //msql.query( "SELECT * FROM `resultset-counts` WHERE `dt` <= '"+sdf.format(cal.getTime())+"'  LIMIT 0, 30" );
      msql.query( "SELECT * FROM `resultset-counts` ORDER BY `id` DESC LIMIT 0, 30" );
      int s_id = 0;
      while(msql.next()){
        int id = msql.getInt("id");
        String time = msql.getString("dt");
        String hostname = msql.getString("hostname");
        String device_type = msql.getString("device_type");
        String mac_address = msql.getString("mac_address");
        Node each = new Node( id,
                              time,
                              hostname,
                              device_type,
                              mac_address
                            );
        this.m_nodes.add(each);
      }
    }
    */
  }
  
  ArrayList<Sniffer> getSniffers(){
    return this.m_sniffers;
  }
  ArrayList<Node> getNodes(){
    return this.m_nodes;
  }
  
  Sniffer searchSniffer(String hostname){
    for (Sniffer sniffer : m_sniffers){
      if (sniffer.getHostname().equals(hostname)){
        return sniffer;
      }
    }
    return m_sniffers.get(1);
  }
  
  void draw(){
    //// PROBLEM ! we are always getting the latest 30 result 
    // Get update nodes
    // > time - 5 mins unique
    //this.m_nodes.clear();
    
    if ( this.msql.connect() ){
      //msql.query( "SELECT DISTINCT * FROM `resultset-counts` WHERE `dt` <= '"+sdf.format(cal.getTime())+"'  LIMIT 0, 30" );
      this.msql.query( "SELECT DISTINCT * FROM `resultset-counts` ORDER BY `id` DESC LIMIT 0, 30" );
      int s_id = 0;
      while(msql.next()){
        int id = msql.getInt("id");
        String time = msql.getString("dt");
        String hostname = msql.getString("hostname");
        String device_type = msql.getString("device_type");
        String mac_address = msql.getString("mac_address");
        boolean foundNode = false;
        for(Node node : m_nodes) {
          if(node.getMac().equals(mac_address)){
             node.setHost(searchSniffer(hostname));
             foundNode = true;
          }
        }
        if (!foundNode){
          Node each = new Node( id,
                                time,
                                hostname,
                                device_type,
                                mac_address
                              );
          this.m_nodes.add(each); 
        }
      }
    }
  }
}
